<?php
defined('IN_IXDCW') or exit('Access Denied');
$do = new AutoInsuranceModule();
$table = Table::name(Table::NAME_AUTO_INSURANCE);
$menus = array (
	array('车险列表', '?moduleid='.$moduleid.'&file=insurance'),
);


if(in_array($action, array('', 'check', 'reject', 'recycle'))) {
	$sfields = array( '姓名', '会员ID', '会员名');
	$dfields = array( 'contact_name', 'user_id', 'user_name');
	$sorder  = array( '添加时间降序', '添加时间升序', '信息ID降序', '信息ID升序');
	$dorder  = array( 'add_time DESC', 'add_time ASC', 'id DESC', 'id ASC');

	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;
	$level = isset($level) ? intval($level) : 0;

	isset($datetype) && in_array($datetype, array('edittime', 'add_time')) or $datetype = 'add_time';
	$fromtime = $fromdate ? strtotime($fromdate.' 0:0:0') : 0;
	$totime = $todate ? strtotime($todate.' 23:59:59') : 0;


	$fields_select = dselect($sfields, 'fields', '', $fields);
	$order_select  = dselect($sorder, 'order', '', $order);
	$condition = '1=1';
	if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	if($psize) $do->setPagesize($psize);
	if($fromtime) $condition .= " AND `$datetype`>=$fromtime";
	if($totime) $condition .= " AND `$datetype`<=$totime";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
}



switch($action) {
	case 'show':
		if(isset($id)) {
			$do->setItemid($id);
			$r = $do->getOne();
		}
		extract($r);
		$content = json_decode($content,true);
		include tpl('auto_insurance_show', $module);
		break;

	case 'delete':
		$id = implode($id,',');
		if(!$db->query("delete from $table where id in($id)")) exit($db->error());
		dmsg('删除车险申请成功！', $forward);
		break;

	case 'export':
		$id or msg('请选择客户信息');
		if(is_array($id)){
			$id = implode(',',$id);
			$lists = $db->getAll("select * from $table where id in ($id)");
		}

		$set['author']      =   'ixdcw';
		$set['title']       =   '车险客户信息';
		$set['intru']       =   '车险客户信息';
		$set['category']    =   '车险客户信息';
		$set['stitle']      =   '车险客户信息';

		$firstline['A']     =   'id|编号';
		$firstline['B']     =   'contact_name|客户姓名';
		$firstline['C']     =   'contact_telephone|联系电话';
		$firstline['D']     =   'contact_address|地址';
		$firstline['E']     =   'model_name|车款名称';
		$firstline['F']     =   'buy_time|购买时间';
		$firstline['G']     =   'close_time|保险到期时间';
		$firstline['H']     =   'accident|是否出现事故';
		$firstline['I']     =   'clrar_preferential|已知优惠';
		$firstline['J']     =   'quotation_company|报价单位';
		$firstline['K']     =   'remark|备注';
		$firstline['L']     =   'insurance_category|购买险种';
		$firstline['M']     =   'user_id|申请会员ID';
		$firstline['N']     =   'user_name|申请会员';
		$firstline['O']     =   'add_time|报名时间';

		foreach($lists as $k=>$t){
			if(OwnCardModule::checkExpire($t['user_name'])){
				$lists[$k]['contact_name'] = $t['contact_name'].'(讯都车主)';
			}
			$lists[$k]['add_time'] = date('Y-m-d H:i:s',$t['add_time']);
		}

		$time = date('Y-m-d',$DT_TIME);
		$filename = '车险_'.$time.'.xls';
		if(Str::exportExcel($lists,$set,$firstline,$filename)){
			dmsg('导出成功', '?moduleid='.$moduleid.'&file='.$file);
		};
		break;

	default:
		$lists = $do->getList($condition, $dorder[$order]);
		$menuid = 0;
		include tpl('insurance', $module);
		break;
}
?>